<template>
  <div class="container">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-row type="flex" justify="space-between" style="height: 50px">
          <el-form-item label="关键字" style="margin-left: 30px">
            <el-input
              v-model="formInline.keyword"
              placeholder="根据编号搜索"
              class="top-left-input"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="formInline.keyword = ''"
              >清除</el-button
            >
            <el-button size="small" type="primary" @click="onSubmit"
              >搜索</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
      <el-alert
        :title="`数据一共 ${tableData.counts} 条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-table
        :data="tableData.items"
        style="width: 100%; margin-top: 15px"
        :header-cell-style="{ backgroundColor: '#fafafa' }"
      >
        <el-table-column prop="id" label="编号" width="220"> </el-table-column>
        <el-table-column label="题型" width="80">
          <template slot-scope="{ row }">
            {{ row.questionType | getQuestionType }}
          </template>
        </el-table-column>
        <el-table-column prop="catalogID" label="题目编号" width="220">
          <template slot-scope="{ row }">
            <a
              v-for="(item, i) in row.questionIDs"
              style="display: block; color: #5f9dff"
              :key="i"
              @click="previewDetails(item.id)"
              >{{ item.number }}</a
            >
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="180">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)" width="166">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)" width="166">
        </el-table-column>
        <el-table-column prop="userName" label="录入人" width="165">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              circle
              style="color: #f56c6c; background: #fef0f0; border-color: #fbc4c4"
              @click="del(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :tableData="tableData" @changePage="changePage"></pagination>
      <titlePreview
        :preview="preview"
        :showDialog.sync="showDialog"
      ></titlePreview>
    </el-card>
  </div>
</template>

<script>
import titlePreview from '@/module-hmmm/components/titlePreview'
import { randoms, detail } from '@/api/hmmm/questions.js'
import pagination from '@/module-hmmm/components/pagination'
export default {
  components: {
    pagination,
    titlePreview
  },
  data () {
    return {
      formInline: {
        keyword: '',
        page: 1,
        pagesize: 5
      },
      tableData: {
        items: [{}]
      },
      showDialog: false,
      preview: {}
    }
  },
  created () {
    this.onSubmit()
  },
  methods: {
    async previewDetails (id) {
      const { data } = await detail({ id })
      this.preview = data
      this.showDialog = true
    },
    async onSubmit () {
      const { data } = await randoms(this.formInline)
      this.tableData = data
    },
    async del (id) {
      await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      // await removeRandoms(id)
      const items = this.tableData.items.filter((item) => {
        return item.id !== id
      })
      this.tableData = {
        ...this.tableData,
        items,
        counts: this.tableData.counts - 1
      }
      this.$message.success('删除成功')
    },
    changePage (page) {
      this.formInline.page = page.page
      this.formInline.pagesize = page.pagesize
      this.onSubmit()
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  margin: 10px;
  .top-left-input {
    font-size: 12px;
    width: 240px;
    ::v-deep .el-input__inner {
      height: 32px;
      line-height: 32px;
    }
  }
}
</style>
